import { bus } from "../../network/index"

// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {
      nameLike: '',
      orderBy: ''
    },
    list: [],
    flag: true,
    title: [
      { text: "综合", status: true },
      { text: "新品", status: false },
      { text: "销量", status: false },
      { text: "价格", status: false },
    ],
  },
  change() {
    this.setData({
      flag: !this.data.flag
    })
  },
  check(event) {
    let index = event.currentTarget.dataset.index
    this.data.title.forEach(element => {
      element.status = false
    });
    this.data.title[index].status = true;

    switch (index) {
      case 0:
        this.data.info.orderBy = ''
        break;
      case 1:
        this.data.info.orderBy = 'addedDown'
        break;
      case 2:
        this.data.info.orderBy = 'ordersUp'
        break;
      case 3:
        this.data.info.orderBy = 'priceUp'
        break;
      default:
        break;
    }
    this.setData({
      title: this.data.title,
      info:this.data.info
    });
    this.getShop()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.info.nameLike = options.val ? options.val : ''
    this.setData({
      info: this.data.info
    })
    this.getShop()
  },
  onSearch(event) {
    this.data.info.nameLike = event.detail
    this.setData({
      info: this.data.info
    })
    this.getShop()
  },
  getShop() {
    bus.goodsList(this.data.info).then((res) => {
      console.log(res.data);
      this.setData({
        list: res.data.data
      })
    }).catch((err) => {
      console.log(err);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})